﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using NetMicro.Core.Ioc.Dependencies;

namespace NetMicro.Domains.Repositories
{
    /// <summary>
    /// 泛型仓储接口
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public interface IRepository<TEntity, TKey> : IRepositoryBase, IScoped where TEntity : EntityBase<TKey>, new()
    {

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool Delete(Expression<Func<TEntity, bool>> whereExpression, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="deleteObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool Delete(TEntity deleteObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool DeleteById(TKey id, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool DeleteByIds(TKey[] ids, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> whereExpression, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="deleteObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(TEntity deleteObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> DeleteByIdAsync(TKey id, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> DeleteByIdsAsync(TKey[] ids, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 假删除
        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool SoftDelete(Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="deleteObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool SoftDelete(TEntity deleteObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键假删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool SoftDeleteById(TKey id, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool SoftDeleteByIds(TKey[] ids, object subTableValue = null, SqlSugarClient client = null);

        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> SoftDeleteAsync(Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="deleteObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> SoftDeleteAsync(TEntity deleteObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键假删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> SoftDeleteByIdAsync(TKey id, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 主键批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> SoftDeleteByIdsAsync(TKey[] ids, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 插入

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="insertObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool Insert(TEntity insertObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="insertObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool InsertRange(List<TEntity> insertObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="insertObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool InsertRange(TEntity[] insertObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 插入返回标识
        /// </summary>
        /// <param name="insertObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        int InsertReturnIdentity(TEntity insertObj, object subTableValue = null, SqlSugarClient client = null);

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="insertObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> InsertAsync(TEntity insertObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="insertObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> InsertRangeAsync(List<TEntity> insertObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="insertObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> InsertRangeAsync(TEntity[] insertObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 插入返回标识
        /// </summary>
        /// <param name="insertObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<int> InsertReturnIdentityAsync(TEntity insertObj, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="columns"></param>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool Update(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool Update(TEntity updateObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="updateObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool UpdateRange(List<TEntity> updateObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="updateObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool UpdateRange(TEntity[] updateObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="columns"></param>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>

        Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync(TEntity updateObj, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="updateObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> UpdateRangeAsync(List<TEntity> updateObjs, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="updateObjs"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> UpdateRangeAsync(TEntity[] updateObjs, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 查询
        #region 获取count
        /// <summary>
        /// 获取行数
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        int Count(Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 获取行数
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 根据id查询
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        TEntity GetById(TKey id, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<TEntity> GetByIdAsync(TKey id, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 是否存在
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        bool IsAny(Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<bool> IsAnyAsync(Expression<Func<TEntity, bool>> whereExpression = null, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 获取list
        /// <summary>
        /// 获取list
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="selector"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        List<TEntity> GetList(Expression<Func<TEntity, bool>> whereExpression = null, Expression<Func<TEntity, TEntity>> selector = null, object subTableValue = null, SqlSugarClient client = null);

        /// <summary>
        /// 获取list
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="selector"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> whereExpression = null, Expression<Func<TEntity, TEntity>> selector = null, object subTableValue = null, SqlSugarClient client = null);
        #endregion

        #region 分页获取

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="orderByType"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="selector"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        PagerList<TEntity> GetPageList(Expression<Func<TEntity, bool>> whereExpression = null, int PageIndex = 1, int PageSize = 20, OrderByType orderByType = OrderByType.Asc, Expression<Func<TEntity, object>> orderByExpression = null, Expression<Func<TEntity, TEntity>> selector = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="conditionalList"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="orderByType"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="selector"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        PagerList<TEntity> GetPageList(List<IConditionalModel> conditionalList, int PageIndex = 1, int PageSize = 20, OrderByType orderByType = OrderByType.Asc, Expression<Func<TEntity, object>> orderByExpression = null, Expression<Func<TEntity, TEntity>> selector = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="orderByType"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="selector"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<PagerList<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression = null, int PageIndex = 1, int PageSize = 20, OrderByType orderByType = OrderByType.Asc, Expression<Func<TEntity, object>> orderByExpression = null, Expression<Func<TEntity, TEntity>> selector = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="conditionalList"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="orderByType"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="selector"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<PagerList<TEntity>> GetPageListAsync(List<IConditionalModel> conditionalList, int PageIndex = 1, int PageSize = 20, OrderByType orderByType = OrderByType.Asc, Expression<Func<TEntity, object>> orderByExpression = null, Expression<Func<TEntity, TEntity>> selector = null, object subTableValue = null, SqlSugarClient client = null);

        #endregion

        #region 获取单条
        /// <summary>
        /// 获取第一条
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="selector"></param>
        /// <param name="orderByType"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        TEntity First(Expression<Func<TEntity, bool>> whereExpression = null, Expression<Func<TEntity, TEntity>> selector = null, OrderByType orderByType = OrderByType.Asc, Expression<Func<TEntity, object>> orderByExpression = null, object subTableValue = null, SqlSugarClient client = null);
        /// <summary>
        /// 获取第一条
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="selector"></param>
        /// <param name="orderByType"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="subTableValue"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> whereExpression = null, Expression<Func<TEntity, TEntity>> selector = null, OrderByType orderByType = OrderByType.Asc, Expression<Func<TEntity, object>> orderByExpression = null, object subTableValue = null, SqlSugarClient client = null);
        #endregion
        #endregion

    }
}
